﻿using HslCommunication.Core;
using PLC.Domain.Dto;
using PLC.Domain.Entity;
using PLC.Domain.Enum;
using PLC.Domain.Models;
using PLC.Domain.Service.IRepository;

namespace PLC.Domain.ScheduledTasks.Strategy.Read;
/// <summary>
/// 读取膜电极和双极板的码
/// </summary>
public class ReadCodeData : BaseRead
{
    private readonly IPLCRepository _plcRepository;


    public ReadCodeData(IPLCRepository plcRepository)
    {
        _plcRepository = plcRepository ?? throw new ArgumentNullException(nameof(plcRepository));
    }
    /// <summary>
    /// 数据组的标志
    /// </summary>
    public override EnumReadGroup ReadGroup { get; protected set; } =EnumReadGroup.二维码;

    public override void Read(IReadWriteNet readWriteNet, PLCConfig plcConfig, List<DtoDataConfig> readConfigList, PLCReadModel plcReadModel)
    {
        if (readConfigList.Count == 0)
        {
            return;
        }

        var barcodeData = new DtoBarcodeData()
        {
            DeviceId = plcConfig.DeviceConfig.DeviceId,
            DeviceType = plcConfig.DeviceConfig.DeviceType
        };
        foreach (var config in readConfigList)
        {
            var data = _plcRepository.ReadData(readWriteNet, config.Register, (EnumDataType)config.DataType,
                 (EnumDataEncoding)(config.Encoding ?? 0), config.DataLength);
            if (config.CodeType==null)
            {
                continue;
            }else if (config.CodeType == (int)EnumCodeType.下端板码)
            {
                barcodeData.SetLowerEndPlateCode(data);
            }else if (config.CodeType == (int)EnumCodeType.上端板码)
            {
                barcodeData.SetUpperEndPlateCode(data);
            }else if (config.CodeType == (int)EnumCodeType.双极板二维码)
            {
                barcodeData.SetBipolarPlate(data);
            }else if (config.CodeType == (int)EnumCodeType.膜电极二维码)
            {
                barcodeData.SetMembraneElectrode(data);
            }

           
        }

        barcodeData.Build();
        plcReadModel.BarcodeDatas.Add(barcodeData);
    }
}